package logic

import (
	"context"
	"goods_srv/mobile/mysql"

	"goods_srv/goods"
	"goods_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsListLogic) GoodsList(in *goods.GoodsListRequest) (*goods.GoodsListResponse, error) {
	// todo: 商品列表

	g := mysql.Goods{}

	list, err := g.GoodsList(in.Keyword, int(in.Page), int(in.PageSize))
	if err != nil {
		logx.Error("查询列表失败", err)
		return nil, err
	}

	var goodsList []*goods.GoodsTime
	for _, v := range list {
		goodsList = append(goodsList, &goods.GoodsTime{
			GoodsID:     int64(v.ID),
			GoodsTitle:  v.GoodsTitle,
			GoodsPrice:  float32(v.GoodsPrice),
			GoodsStock:  v.GoodsStock,
			GoodsStatus: v.GoodsStatus,
			GoodsCover:  v.GoodsCover,
		})
	}

	return &goods.GoodsListResponse{GoodsList: goodsList}, nil
}
